<?php 
require_once (dirname(__FILE__)."/services.php");
class dictionary_RouterExtension extends Extension {
    public function apply(&$context, Chain $chain) {
        $context['rule']['{^/dictionary/.*$}'] = dirname(__FILE__)."/actions.php";
        $chain->next($context);
    }
}
class dictionary_PrivilegeExtension extends Extension {
    public function apply(&$context, Chain $chain) {
        $context['items']['dictionary']['text'] = "Dictionary";
        $context['items']['dictionary']['items']['edit']['text'] = "Edit the dictionary";
        $chain->next($context);
    }
}
class dictionary_InterceptorExtension extends Extension {
    public function apply(&$context, Chain $chain) {
        $context['rule']['{^/dictionary/.*$}'][] = new dictionary_LoginInterceptor();
		$context['rule']['{^/dictionary/new/.*$}'][] = new dictionary_EditorInterceptor();
		$context['rule']['{^/dictionary/([^/]+)/edit/$}'][] = new dictionary_EditorInterceptor();
		$context['rule']['{^/dictionary/delete/$}'][] = new dictionary_EditorInterceptor();
		$context['rule']['{^/dictionary/([^/]+)/sources/$}'][] = new dictionary_EditorInterceptor();
		$context['rule']['{^/dictionary/conflict/$}'][] = new dictionary_EditorInterceptor();
		$context['rule']['{^/dictionary/deleteterm/$}'][] = new dictionary_EditorInterceptor();
		$context['rule']['{^/dictionary/definition/merge/$}'][] = new dictionary_EditorInterceptor();
		$context['rule']['{^/dictionary/definition/split/$}'][] = new dictionary_EditorInterceptor();
		$context['rule']['{^/dictionary/definition/sort/$}'][] = new dictionary_EditorInterceptor();
        $chain->next($context);
    }
}
class dictionary_LoginInterceptor extends Interceptor {
    public function intercept(&$context, InterceptorChain $chain) {
        global $mvc,$featureManager;
        global $currentUser;
        $currentUser = session_getCurrentUser();
        if (isset($currentUser)) {
            $chain->next($context);
        } else {
            $mvc = new MVC(array());
            $mvc->setMessage("message", "Please login first!");
            $currentURI = Utils::getURI();
            $mvc->redirect(REQUEST_BASE.'login'.$currentURI);
        }
    }
}
class dictionary_EditorInterceptor extends Interceptor {
    public function intercept(&$context, InterceptorChain $chain) {
        global $mvc,$featureManager;
        global $currentUser;
        $currentUser = session_getCurrentUser();
        if (isset($currentUser)) {
            $userContext = array("user"=>$currentUser['serial']);
            $featureManager->apply("com.yizin.platform.role.get.by.user", $userContext);
            $roleContext = array('role'=>$userContext['role']['serial'], 'id'=>'dictionary.edit');
            $featureManager->apply("com.yizin.platform.privilege.get.by.id", $roleContext);
            if (!($roleContext['allow'])) {
                $mvc = new MVC(array());
                $mvc->setMessage("message", "Permission Deny.");
                $currentURI = Utils::getURI();
                $mvc->redirect(REQUEST_BASE.'login'.$currentURI);
            };
        }
        $chain->next($context);
    }
}
class dictionaryFeature extends Feature {
    public function registerExtension() {
        $this->extensions = Array( 
              	 new dictionary_RouterExtension("com.yizin.platform.router"),
				 new dictionary_InterceptorExtension("com.yizin.platform.interceptor"),
                 new dictionary_PrivilegeExtension("com.yizin.platform.privilege"),
		);
    }
    
    public function registerExtendable() {
    }
    
    public function init() {
    	$conn = getDBConnection();
		$conn->query('drop table if exists dictionaries;');
		$conn->query('drop table if exists dictionary_terms;');
		$conn->query('drop table if exists dictionary_sources;');
		$conn->query('drop table if exists dictionary_term_definitions;');
		$conn->query('drop table if exists dictionary_conflict_definitions;');
    	$conn->query('
		create table dictionaries
		(
			serial bigint primary key auto_increment,
			title varchar(255) not null,
			description varchar(255) null,
			creator bigint not null,
			create_date datetime not null,
			last_editor bigint null,
			last_edit_date datetime null
		) CHARSET=utf8;
		');
		$conn->query('
		create table dictionary_terms
		(
			serial bigint primary key auto_increment,
			term varchar(255) not null,
			creator bigint not null,
			create_date datetime not null,
			last_editor bigint null,
			last_edit_date datetime null,
			belongto bigint not null
		) CHARSET=utf8;
		');
		$conn->query('
		create table dictionary_sources
		(
			serial bigint primary key auto_increment,
			dictionary bigint not null,
			source bigint not null
		) CHARSET=utf8;
		');
		$conn->query('
		create table dictionary_term_definitions
		(
			serial bigint primary key auto_increment,
			term bigint not null,
			definition varchar(255) not null,
			comefrom bigint null,
			inorder int not null,
			mergedby bigint null,
			version int not null
		) CHARSET=utf8;
		');
		$conn->query('
		create table dictionary_conflict_definitions
		(
			serial bigint primary key auto_increment,
			term bigint not null,
			definition varchar(255) not null,
			comefrom bigint null,
			version int not null
		) CHARSET=utf8;
		');
		$conn->disconnect();
	}
    
    public function destroy() {
    	$conn=getDBConnection();
		$conn->query('drop table if exists dictionaries;');
		$conn->query('drop table if exists dictionary_terms;');
		$conn->query('drop table if exists dictionary_sources;');
		$conn->query('drop table if exists dictionary_term_definitions;');
		$conn->query('drop table if exists dictionary_conflict_definitions;');
		$conn->disconnect();
    }
}

$featureClass = "dictionaryFeature";
$featureName = "Dictionary";
$featureIntroduction = "dictionary feature";
?>
